package com.cx.mall.web.service.impl;

import com.alibaba.fastjson2.JSON;
import com.cx.mall.api.SeckillGoodsFeign;
import com.cx.mall.api.goods.CategoryFeign;
import com.cx.mall.api.goods.ProductFeign;
import com.cx.mall.common.model.goods.CategoryDTO;
import com.cx.mall.common.model.goods.ProductDetailDTO;
import com.cx.mall.common.model.seckill.SeckillGoods;
import com.cx.mall.common.utils.ResponseEnum;
import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.web.service.IPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements IPageService {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private SeckillGoodsFeign seckillGoodsFeign;
    @Value("${static-path}")
    private String path;
    @Override
    public void createProductTemplate(Long id) throws Exception {
        // 1 创建Thymeleaf模版的上下文对象
        Context context = new Context();
        // 2设置模板数据
        context.setVariables(getTemplateData(id));
        // 3 指定静态页面保存路径和名称（以spuId命名）
        File file = new File(path,id +".html");
        PrintWriter printWriter = new PrintWriter( file, "UTF-8");
        // 4 静态页面生成
        templateEngine.process("item-template",context,printWriter);
    }
    /**
     * 获取模板数据
     */
    private Map<String,Object> getTemplateData(Long id) throws Exception {
        Map<String,Object> model = new HashMap<>();
        // 1 远程调用商品服务查询商品详情
        ResponseVO result = productFeign.getById(id);
        if(result.getCode() != ResponseEnum.SUCCESS.getCode() || result.getData() == null){
            throw new Exception(result.getMessage());
        }
        ProductDetailDTO data = JSON.parseObject(JSON.toJSONString(result.getData()), ProductDetailDTO.class);
        model.put("goodsdetail",data);
        // 2 远程调用类别信息
        ResponseVO resultCategory = categoryFeign.list();
        if(resultCategory.getCode() != ResponseEnum.SUCCESS.getCode() || resultCategory.getData() == null){
            throw new Exception(resultCategory.getMessage());
        }
        List<CategoryDTO> list = JSON.parseArray(JSON.toJSONString(resultCategory.getData()), CategoryDTO.class);
        model.put("categoryList",list);
        return model;
    }

    @Override
    public void createSeckillProductTemplate(Integer activityId) throws Exception {
        // 1 创建Thymeleaf模版的上下文对象
        Context context = new Context();
        // 2 获取模板数据
        ResponseVO responseVO = seckillGoodsFeign.listByActivityId(activityId);
        if(responseVO.getCode() != ResponseEnum.SUCCESS.getCode() || responseVO.getData() == null){
            throw new Exception(responseVO.getMessage());
        }
        List<SeckillGoods> goodsList = JSON.parseArray(JSON.toJSONString(responseVO.getData()), SeckillGoods.class);
        for (SeckillGoods goods : goodsList) {
            // 2设置模板数据
            context.setVariable("skuInfo", goods);
            // 3 指定静态页面保存路径和名称（以activityId-skuId命名）
            File file = new File(path,String.format("%s-%s.html",activityId,goods.getSkuId()));
            PrintWriter printWriter = new PrintWriter( file, "UTF-8");
            // 4 静态页面生成
            templateEngine.process("seckillitem-template",context,printWriter);
        }
    }


}
